package com.zzk.lanouhn.myapplication1.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.zzk.lanouhn.myapplication1.R;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.tencent.qq.QQ;

/**
 * Created by lanouhn on 16/9/13.
 */
public class LoginActivity extends AppCompatActivity {
    private ImageView iv_fanhui, iv_denglu;
    private EditText et_zhanghao, et_mima;
    private Button btn_register;
    private ImageView log_QQ;
    private PlatFromLis platFromLis = new PlatFromLis();

    private SharedPreferences sp;
    private SharedPreferences.Editor editor;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_activity);
        initid();
        initDate();
    }

    private void initid() {
        iv_fanhui = (ImageView) findViewById(R.id.long_iv_fanhui);
        iv_denglu = (ImageView) findViewById(R.id.log_iv_denglu);
        et_zhanghao = (EditText) findViewById(R.id.log_et_zhanghao);
        et_mima = (EditText) findViewById(R.id.log_et_mima);
        btn_register = (Button) findViewById(R.id.log_btn_register);
        log_QQ = (ImageView) findViewById(R.id.login_iv_QQ);

        //初始化
        sp = getSharedPreferences("loginpic", MODE_PRIVATE);
        editor = sp.edit();
    }

    private void initDate() {
        //返回
        iv_fanhui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
        });
        //正常登录
        iv_denglu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (et_zhanghao.getText().toString().length() < 8) {
                    Toast.makeText(LoginActivity.this, "输入账号有误", Toast.LENGTH_SHORT).show();
                } else {
                    if (et_mima.getText().toString().length() < 6) {
                        Toast.makeText(LoginActivity.this, "密码错误", Toast.LENGTH_SHORT).show();
                    } else {
                        editor.putString("shifou", "true");
                        editor.commit();
                        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                        startActivity(intent);
                        finish();
                    }
                }
            }
        });
        //QQ登录
        log_QQ.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                ShareSDK.initSDK(LoginActivity.this);

                Platform qq = ShareSDK.getPlatform(QQ.NAME);

                if (qq.isValid()) {
                    qq.removeAccount();
                }

                qq.setPlatformActionListener(platFromLis);
                qq.SSOSetting(true);

                authorize(qq);

                //finish();


//                ShareSDK.initSDK(LoginActivity.this);
//                Platform weibo = ShareSDK.getPlatform(QQ.NAME);
//

//
//
//                weibo
//                weibo.authorize();//单独授权
//                weibo.showUser(null);//授权并获取用户信息
                //authorize与showUser单独调用一个即可
                //移除授权
                //weibo.removeAccount(true);
//                authorize(weibo);
            }
        });
    }


    //QQ登录返回的接口
    private class PlatFromLis implements PlatformActionListener {

        @Override
        public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
            //成功
            Iterator ite = hashMap.entrySet().iterator();
            while (ite.hasNext()) {
                Map.Entry entry = (Map.Entry) ite.next();
                Object key = entry.getKey();
                Object value = entry.getValue();
                System.out.println(key + "： " + value);
            }
//            editor.putString("shifou", "true");
//            editor.commit();
            Log.i("aaa", "成功了");


            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    editor.putString("shifou", "true");
                    editor.commit();
                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                    startActivity(intent);

                }
            });

            // finish();
        }

        @Override
        public void onError(Platform platform, int i, Throwable throwable) {
            //失败
            Log.i("aaa", "失败");
        }

        @Override
        public void onCancel(Platform platform, int i) {
            //取消
            Log.i("aaa", "取消");
        }
    }

    private void authorize(Platform plat) {
        if (plat == null) {
            return;
        }
        //判断指定平台是否已经完成授权
        if (plat.isAuthValid()) {
            String userId = plat.getDb().getUserId();
            Log.i("result", "isAuthValid");
            if (userId != null) {
                Log.i("result", "userId");
                return;
            }
        }
        // true不使用SSO授权，false使用SSO授权
        plat.SSOSetting(true);
        //获取用户资料
        plat.showUser(null);
    }


}
